using System.Collections.Generic;
using UnityEngine;

public class BotHoleController : HoleController
{
	protected Transform _cachedTransform;

	protected float changeTargetTimer;

	protected FallingObjectBehaviour targetObject;

	protected Transform target;

	protected Transform farTarget;

	protected float _sensitivity = 9f;

	private void Start()
	{
		_hole.IsBot = true;
		_cachedTransform = base.transform;
	}

	public void SetDirection(Vector3 dir)
	{
		Quaternion rotation = Quaternion.Euler(new Vector3(0f, MathUtils.Vector2ToAngle(new Vector2(0f - dir.x, dir.z)), 0f));
		_hole.SetRotation(rotation);
	}

	protected void GoToFurthestTarget()
	{
		if (farTarget == null)
		{
			farTarget = FindFurthestTarget();
			if ((bool)farTarget)
			{
				changeTargetTimer = Time.time + 5f;
			}
		}
		if ((bool)farTarget)
		{
			Vector3 vector = Vector3.MoveTowards(_cachedTransform.position, farTarget.position, Time.deltaTime * _hole.Speed * _sensitivity);
			Vector3 position = _cachedTransform.position;
			vector.y = position.y;
			SetDirection(vector);
			_cachedTransform.position = vector;
			if (changeTargetTimer - Time.time < 0f)
			{
				farTarget = null;
			}
		}
	}

	protected Transform FindFurthestTarget()
	{
		HashSet<FallingObjectBehaviour> currentFallingObjects = BaseGameManager.Instance.CurrentFallingObjects;
		FallingObjectBehaviour fallingObjectBehaviour = null;
		foreach (FallingObjectBehaviour item in currentFallingObjects)
		{
			if (!(item == null) && item.IsValidTarget(_hole.Level) && (fallingObjectBehaviour == null || (item.GetTargetTransform().position - _cachedTransform.position).sqrMagnitude > (fallingObjectBehaviour.GetTargetTransform().position - _cachedTransform.position).sqrMagnitude))
			{
				fallingObjectBehaviour = item;
			}
		}
		if (fallingObjectBehaviour == null)
		{
			return null;
		}
		float num = (fallingObjectBehaviour.GetTargetTransform().position - _cachedTransform.position).sqrMagnitude - Random.Range(150f, 500f);
		List<FallingObjectBehaviour> list = new List<FallingObjectBehaviour>();
		foreach (FallingObjectBehaviour item2 in currentFallingObjects)
		{
			if (!(item2 == null) && item2.IsValidTarget(_hole.Level) && (item2.GetTargetTransform().position - _cachedTransform.position).sqrMagnitude > num)
			{
				list.Add(item2);
			}
		}
		if (list.Count > 0)
		{
			return list[Random.Range(0, list.Count)].GetTargetTransform();
		}
		return null;
	}

	protected Transform FindClosestTarget()
	{
		HashSet<FallingObjectBehaviour> currentFallingObjects = BaseGameManager.Instance.CurrentFallingObjects;
		FallingObjectBehaviour fallingObjectBehaviour = null;
		foreach (FallingObjectBehaviour item in currentFallingObjects)
		{
			if (!(item == null) && item.IsValidTarget(_hole.Level) && (fallingObjectBehaviour == null || (item.GetTargetTransform().position - _cachedTransform.position).sqrMagnitude < (fallingObjectBehaviour.GetTargetTransform().position - _cachedTransform.position).sqrMagnitude))
			{
				fallingObjectBehaviour = item;
			}
		}
		if (fallingObjectBehaviour == null)
		{
			return null;
		}
		float num = (fallingObjectBehaviour.GetTargetTransform().position - _cachedTransform.position).sqrMagnitude + (float)Random.Range(200, 500);
		List<FallingObjectBehaviour> list = new List<FallingObjectBehaviour>();
		foreach (FallingObjectBehaviour item2 in currentFallingObjects)
		{
			if (!(item2 == null) && item2.IsValidTarget(_hole.Level) && (item2.GetTargetTransform().position - _cachedTransform.position).sqrMagnitude < num)
			{
				list.Add(item2);
			}
		}
		if (list.Count > 0)
		{
			return list[Random.Range(0, list.Count)].GetTargetTransform();
		}
		return null;
	}

	protected bool AreHoleAround()
	{
		List<Hole> currentHoles = BaseGameManager.Instance.CurrentHoles;
		foreach (Hole item in currentHoles)
		{
			if (!(item == null) && !(item == _hole) && !item.IsDead && item.Size != _hole.Size && (item.transform.position - _cachedTransform.transform.position).sqrMagnitude < 25f)
			{
				return true;
			}
		}
		return false;
	}

	public bool DestroyTargetIfOutOfBounds()
	{
		Vector3 position = target.position;
		if (position.y < -300f)
		{
			targetObject.ForceDisableObject();
			target = null;
			return true;
		}
		return false;
	}
}
